package com.qingxin.cms.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import com.qingxin.cms.entity.CmsCategory;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
/**
 * 文章类别
 * @author Administrator
 *
 */
@Service
public class CmsCategoryService extends BaseService<CmsCategory, String> {
	/**
	 * 获取菜单树数据
	 * @param loginUser
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<JsTreeNode> getUITreeData(LoginUser loginUser){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.cms.entity.CmsCategory(categoryId,categoryName)");
		hql.append(" from CmsCategory");
		
		if("2".equals(loginUser.getUser_type())){
			hql.append(" where isPark='1' ");
		}
		
		hql.append(" order by categoryName asc");
		List<CmsCategory> categorys = listByHql(hql.toString());
		if(categorys != null){
			for(CmsCategory category : categorys){
				list.add(new JsTreeNode(category.getCategoryId(),category.getCategoryName(),"ARTICLE",null,false));
			}
		}
		return list;
	}
	
	
	/**
	 * 获类别列表分页数据
	 * @param parkName
	 * @param parkStatus
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(PageParam pageParam,String search,String leftdate, LoginUser loginUser,HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.cms.entity.CmsCategory(" +
				"categoryId,categoryCode,categoryName,createBy,createTime,updateBy,updateTime)" +
				" from CmsCategory");
		if("2".equals(loginUser.getUser_type())){
			querier.append(" where isPark='1' ");
			if(StringUtils.isNotEmpty(search)){
				search = "%"+search.replace("'", "")+"%";
				querier.append(" and (categoryName like :categoryName)");
				querier.setParam("categoryName", search);
			}
		}else{
			if(StringUtils.isNotEmpty(search)){
				search = "%"+search.replace("'", "")+"%";
				querier.append(" where (categoryName like :categoryName)");
				querier.setParam("categoryName", search);
			}
		}
		querier.append(pageParam.getOrderby(" order by createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	/**
	 * 删除类别信息 
	 * @param categoryId
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError delete(String[] categoryId, LoginUser loginUser, HttpServletRequest request){
		for(String id : categoryId){
			CmsCategory category = get(id);
			if(category==null){
				continue;
			}
			if ("0".equals(category.getIsSys())) {
				return new SysError("类别 “"+category.getCategoryName()+"”为系统保留，不能删除.");
			}
			Object obj = getByHql("select articleId from CmsArticle where cmsCategory.categoryId=?", id);
			if(obj!=null){
				return new SysError("类别 “"+category.getCategoryName()+"”下有文章信息，不能删除.");
			}
			this.delete(category);
		}
		return null;
	}
	
	/**
	 * 添加类别
	 * @param category
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(CmsCategory category,LoginUser loginUser, HttpServletRequest request){
		Date now = new Date();
		category.setCreateTime(now);
		category.setUpdateTime(now);
		category.setCreateBy(loginUser.getLogin_name());
		category.setUpdateBy(loginUser.getLogin_name());
		category.setCmsArticles(null);
		category.setIsSys("1");
		this.save(category);
		return null;
	}
	
	/** 
	 * 修改类别
	 * @param category
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(CmsCategory category, LoginUser loginUser, HttpServletRequest request){
		CmsCategory old = getSafeEntity(category.getCategoryId());
		String fields = "categoryCode,categoryName";
		BeanUtil.setTargetFields(old, category, fields, true);
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		Updater<CmsCategory> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		return null;
	}
	
}
